package cn.myzqu.kafka.demo;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;

import java.time.Duration;
import java.util.Collections;
import java.util.Properties;

/**
 * 消费者
 *
 * @author guoqiujing
 * @date 2020/11/25 14:13
 */
public class ConsumerFastStart {

    private static final String brokerList = "172.168.50.171:9092";
    private static final String topic = "topic-demo";

    private static final String groupId = "group.demo";

    public static void main(String[] args) {

        Properties properties = new Properties();
        properties.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        properties.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        properties.put("bootstrap.servers", brokerList);
        properties.put("group.id", groupId);
        // 创建一个消费者客户端实例
        KafkaConsumer consumer = new KafkaConsumer(properties);
        // 订阅主题
        consumer.subscribe(Collections.singletonList(topic));
        // 循环消费消息
        while (true) {
            ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(1000));
            for (ConsumerRecord record : records) {
                System.out.println("partition:" + record.partition());
                System.out.println("offset:" + record.offset());
                System.out.println("value:" + record.value());
            }
        }
    }
}
